/*
 * PanelEditMember.java
 *
 * Created on 15 Desember 2011, 21:44
 */
package Boundary;

import Controller.DatabaseHandler;
import Controller.controllerMember;
import java.awt.FlowLayout;
import java.lang.reflect.Member;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  Jeanot Nahasan
 */
public class PanelEditMember extends javax.swing.JPanel {

    JPanel home;
    private int numCols;

    /** Creates new form PanelEditMember */
    public PanelEditMember() {
        initComponents();
    }

    public PanelEditMember(JPanel panel) {
        initComponents();
        this.home = panel;
    }

    public void showTabel() {
        try {

            String query = "select * from member order by id";
            PreparedStatement psmt = DatabaseHandler.getDBConnection().prepareStatement(query);


            ResultSet rs = psmt.executeQuery();
            LinkedList x = new LinkedList();
            while (rs.next()) {
                x.add(rs.getString("id"));
                x.add(rs.getString("namamember"));
                x.add(rs.getString("nohpmember"));
                x.add(rs.getString("alamatemail"));
                x.add(rs.getString("alamatmember"));
            }

            DefaultTableModel df2 = new DefaultTableModel();
            String[] identifier = {"ID", "Nama", "No Hp", "Email", "Alamat"};
            df2.setColumnIdentifiers(identifier);


            while (!x.isEmpty()) {
                String[] data = new String[5];
                for (int i = 0; i < data.length; i++) {
                    data[i] = String.valueOf(x.remove());
                }
                df2.addRow(data);
            }
            tabelMember.setModel(df2);
            tabelMember.getColumnModel().getColumn(0).setPreferredWidth(30);
        } catch (SQLException ex) {
            Logger.getLogger(PanelEditMember.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCari = new javax.swing.JPanel();
        txtCari = new javax.swing.JTextField();
        panelEditDetailMember = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNamaMeber = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtNoHp = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAlamat = new javax.swing.JTextArea();
        lblStatusNoHp = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblNoHp = new javax.swing.JLabel();
        lblImageIcon = new javax.swing.JLabel();
        lblIDMember = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        lblStatusNama = new javax.swing.JLabel();
        lblStatusEmail = new javax.swing.JLabel();
        lblStatusEditMember = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        btnTambah = new javax.swing.JButton();
        btnCari = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelMember = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(960, 540));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        panelCari.setBackground(new java.awt.Color(113, 184, 255));
        panelCari.setVisible(false);

        txtCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCariKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelCariLayout = new org.jdesktop.layout.GroupLayout(panelCari);
        panelCari.setLayout(panelCariLayout);
        panelCariLayout.setHorizontalGroup(
            panelCariLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCariLayout.createSequentialGroup()
                .add(txtCari, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelCariLayout.setVerticalGroup(
            panelCariLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCariLayout.createSequentialGroup()
                .add(txtCari, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(panelCari, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 150, 350, 20));

        panelEditDetailMember.setBackground(new java.awt.Color(0, 102, 255));
        panelEditDetailMember.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Member", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(255, 255, 255)));
        panelEditDetailMember.setVisible(false);
        panelEditDetailMember.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Alamat");
        panelEditDetailMember.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 80, -1, -1));

        txtNamaMeber.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtNamaMeber.setText("nama");
        panelEditDetailMember.add(txtNamaMeber, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 200, -1));

        txtEmail.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtEmail.setText("email");
        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });
        panelEditDetailMember.add(txtEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 120, 200, -1));

        txtNoHp.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtNoHp.setText("no hp");
        panelEditDetailMember.add(txtNoHp, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 40, 150, 20));

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        panelEditDetailMember.add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 100, 120, -1));

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setToolTipText("Klik untuk menyimpan perubahan");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        panelEditDetailMember.add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 40, 120, 50));

        txtAlamat.setColumns(20);
        txtAlamat.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtAlamat.setRows(5);
        jScrollPane1.setViewportView(txtAlamat);

        panelEditDetailMember.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 80, 230, 60));

        lblStatusNoHp.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusNoHp.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusNoHp.setText("*");
        lblStatusNoHp.setToolTipText("wajib diisi. hanya boleh angka");
        panelEditDetailMember.add(lblStatusNoHp, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 40, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Email");
        panelEditDetailMember.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, -1, -1));

        lblNoHp.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblNoHp.setForeground(new java.awt.Color(255, 255, 255));
        lblNoHp.setText("No Hp");
        panelEditDetailMember.add(lblNoHp, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 40, -1, -1));

        lblImageIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/member.png"))); // NOI18N
        panelEditDetailMember.add(lblImageIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        lblIDMember.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblIDMember.setForeground(new java.awt.Color(255, 255, 255));
        lblIDMember.setText("ID");
        panelEditDetailMember.add(lblIDMember, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, -1, -1));

        txtID.setEditable(false);
        txtID.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtID.setText("id");
        panelEditDetailMember.add(txtID, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 40, 150, 20));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Nama");
        panelEditDetailMember.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 80, -1, -1));

        lblStatusNama.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusNama.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusNama.setText("*");
        lblStatusNama.setToolTipText("wajib diisi. hanya huruf. tidak boleh mengandung karakter unik dan/atau angka");
        panelEditDetailMember.add(lblStatusNama, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 80, -1, -1));

        lblStatusEmail.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblStatusEmail.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusEmail.setText("*");
        lblStatusEmail.setToolTipText("wajib diisi");
        panelEditDetailMember.add(lblStatusEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 120, -1, -1));

        lblStatusEditMember.setForeground(new java.awt.Color(255, 255, 255));
        lblStatusEditMember.setText("Status :");
        panelEditDetailMember.add(lblStatusEditMember, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 160, -1, -1));

        add(panelEditDetailMember, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 310, 900, 190));

        btnEdit.setText("Edit");
        btnEdit.setToolTipText("klik untuk mengedit detail member");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        btnEdit.setVisible(false);
        add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 150, 70, -1));

        btnTambah.setText("Tambah");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });
        add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, 110, -1));

        btnCari.setText("Cari");
        btnCari.setToolTipText("Tampilkan field cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });
        add(btnCari, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 150, 70, -1));

        btnHapus.setText("Hapus");
        btnHapus.setVisible(false);
        btnHapus.setToolTipText("Klik untuk menghapus member");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 150, 70, -1));

        tabelMember.setDragEnabled(false);
        tabelMember.setBackground(new java.awt.Color(204, 204, 204));
        tabelMember.setFont(new java.awt.Font("Tahoma", 0, 12));
        tabelMember.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tabelMember.getTableHeader().setReorderingAllowed(false);
        tabelMember.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelMemberMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabelMember);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 180, 730, 120));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/bg.jpg"))); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 960, 540));
    }// </editor-fold>//GEN-END:initComponents
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        String id = txtID.getText();
        String noHp = txtNoHp.getText();
        String nama = txtNamaMeber.getText();
        String alamat = txtAlamat.getText();
        String email = txtEmail.getText();
        Entiti.Member member = new Entiti.Member();

        if (!member.isNamaValid(nama)) {
            lblStatusNama.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusNama.setText("Error!");
            lblStatusEditMember.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusEditMember.setText("Update Member Gagal!");
        } else {
            lblStatusNama.setIcon(null);
            lblStatusNama.setText("");
        }
        if (!member.isNoHpMemberValid(noHp)) {
            lblStatusNoHp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusNoHp.setText("Error!");
            lblStatusEditMember.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            this.lblStatusEditMember.setText("Update Member Gagal!");
        } else {
            lblStatusNoHp.setIcon(null);
            lblStatusNoHp.setText("");
        }
        if (!member.isEmailMemberValid(email)) {
            lblStatusEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusEmail.setText("Error!");
            lblStatusEditMember.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gambar/error.png")));
            lblStatusEditMember.setText("Update Member Gagal!");
        } else {
            lblStatusEmail.setIcon(null);
            lblStatusEmail.setText("");
        }
        if (member.isNamaValid(nama) == true && member.isEmailMemberValid(email) == true && member.isNoHpMemberValid(noHp) == true) {
            controllerMember.updateMember(id, nama, noHp, alamat, email);
            showTabel();
            panelEditDetailMember.setVisible(false);// TODO add your handling code here:
            JOptionPane.showMessageDialog(null, "Data member berhasil di update");
            lblStatusEditMember.setIcon(null);
            lblStatusEditMember.setText("Penambahan Member Baru Berhasil!");

        }
        

    }//GEN-LAST:event_btnSaveActionPerformed

    private void tabelMemberMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMemberMouseClicked
        btnEdit.setVisible(true);
        btnHapus.setVisible(true);
}//GEN-LAST:event_tabelMemberMouseClicked

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_txtEmailActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
      
        panelEditDetailMember.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        controllerMember member = new controllerMember();
        int posR = tabelMember.getSelectedRow();
        int id = 0;
        id = Integer.parseInt(tabelMember.getValueAt(posR, 0).toString().trim());
        try {
            member.hapusMember(id);
            showTabel();
            JOptionPane.showMessageDialog(null, "member terhapus");
            panelEditDetailMember.setVisible(false);// TODO add your handling code here:
        } catch (SQLException ex) {

        }
    }//GEN-LAST:event_btnHapusActionPerformed

    private void txtCariKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCariKeyReleased
        if (!txtCari.getText().matches("")) {
            controllerMember.cariMember(tabelMember, txtCari);
            panelEditDetailMember.setVisible(false);
        }  else {
            showTabel();// TODO add your handling code here:
        }
    }//GEN-LAST:event_txtCariKeyReleased

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        panelEditDetailMember.setVisible(true);
        int posR = tabelMember.getSelectedRow();
        String alamat = null, noHp = null, nama = null, email = null;
        String id = tabelMember.getValueAt(posR, 0).toString().trim();
        alamat = tabelMember.getValueAt(posR, 4).toString().trim();
        noHp = tabelMember.getValueAt(posR, 2).toString().trim();
        nama = tabelMember.getValueAt(posR, 1).toString().trim();
        email = tabelMember.getValueAt(posR, 3).toString().trim();
        txtID.setText(id);
        txtNoHp.setText(noHp);
        txtNamaMeber.setText(nama);
        txtAlamat.setText(alamat);
        txtEmail.setText(email);        // TODO add your handling code here:
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
        panelCari.setVisible(true);    // TODO add your handling code here:
}//GEN-LAST:event_btnCariActionPerformed

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed
        Home.tampilFrameTambah();
    // TODO add your handling code here:
    }//GEN-LAST:event_btnTambahActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCari;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblIDMember;
    private javax.swing.JLabel lblImageIcon;
    private javax.swing.JLabel lblNoHp;
    private javax.swing.JLabel lblStatusEditMember;
    private javax.swing.JLabel lblStatusEmail;
    private javax.swing.JLabel lblStatusNama;
    private javax.swing.JLabel lblStatusNoHp;
    private javax.swing.JPanel panelCari;
    private javax.swing.JPanel panelEditDetailMember;
    private javax.swing.JTable tabelMember;
    private javax.swing.JTextArea txtAlamat;
    private javax.swing.JTextField txtCari;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtNamaMeber;
    private javax.swing.JTextField txtNoHp;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setVisible(true);
        frame.setSize(960, 420);
        PanelEditMember tes = new PanelEditMember();

        frame.add(tes);
        tes.setVisible(true);

    }
}



